

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:get/get.dart';
import 'package:xreader/base/view/base_view.dart';
import 'package:xreader/common/util.dart';
import 'package:xreader/logs/log.dart';
import 'package:xreader/novel/model/novel_content_model.dart';
import 'package:xreader/novel/view/reader/novel_reader_list_page.dart';
import 'package:xreader/novel/viewmodel/novel_content_view_model.dart';

class NovelReaderPage extends BaseView<NovelContentChapterViewModel>{
  final int bookid;
  final int chapterId;
  NovelReaderPage(this.bookid,this.chapterId);
  @override
  Widget buildContent(BuildContext context, NovelContentChapterViewModel viewModel) {
    return Scaffold(
      body: _NovelReaderPageContent(bookid,chapterId),
    );
  }

  @override
  NovelContentChapterViewModel buildViewModel() {
    Log.i("执行----》buildViewModel tag=${tag}");
    return NovelContentChapterViewModel(bookid,chapterId,
        contentParseerModel: NetNovelContentModel());
  }

  @override
  String? get tag => "${bookid}";

}

class _NovelReaderPageContent extends StatefulWidget {
  final int bookid;
  final int chapterId;
  const _NovelReaderPageContent(this.bookid,this.chapterId,{Key? key}) : super(key: key);

  @override
  State<_NovelReaderPageContent> createState() => _NovelReaderPageContentState();
}

class _NovelReaderPageContentState extends State<_NovelReaderPageContent>
with SingleTickerProviderStateMixin {
  late AnimationController menuAnimationController;
  late Animation<Offset> menuTopAnimationProgress;
  late Animation<Offset> menuBottomAnimationProgress;
  final GlobalKey _ignorePointerkey = GlobalKey();
  bool _shouldIgnorePointer = false;

  @override
  void initState() {
    super.initState();
    menuAnimationController = AnimationController(
        vsync: this,
        duration: Duration(microseconds: 300)
    );
    menuTopAnimationProgress = menuAnimationController.drive(
      Tween(begin: Offset(0.0,-1.0),end: Offset.zero)
    );
    menuBottomAnimationProgress = menuAnimationController.drive(
      Tween(begin: Offset(0.0,1.0),end: Offset.zero)
    );
    menuAnimationController.addStatusListener((state) {
      if(state == AnimationStatus.completed){
         setIgnorePointer(true);
      }else{
        setIgnorePointer(false);
      }
    });
  }
  void setIgnorePointer(bool value){
    if(_shouldIgnorePointer == value) return;
    _shouldIgnorePointer = value;
    if(_ignorePointerkey.currentContext != null){
      final RenderIgnorePointer renderBox = _ignorePointerkey.currentContext!.
      findRenderObject()! as RenderIgnorePointer;
      renderBox.ignoring = _shouldIgnorePointer;
    }
  }
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Stack(
        children: [
          //小说内容显示区域
          Positioned.fill(
            child: GestureDetector(
              behavior: HitTestBehavior.translucent,
              onTap: (){
                if(menuAnimationController.isCompleted){
                  menuAnimationController.reverse();
                }else{
                  menuAnimationController.forward();
                }
              },
              child: IgnorePointer(
                key:_ignorePointerkey,
                ignoring: _shouldIgnorePointer,
                //child: _NovelReaderPageContent(widget.bookid),
                child: _NovelReaderPageReaderContent(widget.bookid,widget.chapterId),
              ),
            ),
          ),
          //阅读器页面菜单 -顶部
          Positioned(
            top: 0,
            left: 0,
            right: 0,
            child: SlideTransition(
              position: menuTopAnimationProgress,
              child: _NovelReaderPageMenuTop(),
            ),
          ),
          Positioned(
            bottom: 0,
            left: 0,
            right: 0,
            child: SlideTransition(
              position: menuBottomAnimationProgress,
              child: _NovelReaderPageMenuBottom(widget.bookid),
            ),
          )
        ],
      ) ,
    );
  }
}
// 阅读器页面内容部分
class _NovelReaderPageReaderContent extends StatelessWidget{
  final int bookid;
  final int chapterId;

  const _NovelReaderPageReaderContent(this.bookid,this.chapterId,{Key? key}) :super(key : key);

  @override
  Widget build(BuildContext context) {
    return NovelReaderListPage(bookid,chapterId);
  }

}
//阅读器页面菜单-顶部
class _NovelReaderPageMenuTop extends StatefulWidget {
  const _NovelReaderPageMenuTop({Key? key}) : super(key: key);

  @override
  State<_NovelReaderPageMenuTop> createState() => _NovelReaderPageMenuTopState();
}

class _NovelReaderPageMenuTopState extends State<_NovelReaderPageMenuTop> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: AppBar(),
    );
  }
}

//阅读器页面菜单-底部
class _NovelReaderPageMenuBottom extends StatefulWidget {
  final int bookid;
  const _NovelReaderPageMenuBottom(this.bookid,{Key? key}) : super(key: key);

  @override
  State<_NovelReaderPageMenuBottom> createState() => _NovelReaderPageMenuBottomState();
}

class _NovelReaderPageMenuBottomState extends State<_NovelReaderPageMenuBottom> {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Theme.of(context).appBarTheme.backgroundColor,
      child: Column(
        children: [
          Row(
            children: [
              buildButton(
                  context: context,
                  onPressCallBack: (){

                  },
                  childWidgetBuilder:(context){
                    return Padding(
                      padding: EdgeInsetsDirectional.all(16),
                      child: Text(
                        '上一章',
                        style: TextStyle(
                          fontWeight: FontWeight.bold,
                          fontSize: 16,
                        ),
                      ),
                    );
                  }
              ),
              Expanded(child: Container()),
              buildButton(
                  context: context,
                  onPressCallBack: (){},
                  childWidgetBuilder:(context){
                    return Padding(
                      padding: EdgeInsetsDirectional.all(16),
                      child: Text(
                        '下一章',
                        style: TextStyle(
                          fontWeight: FontWeight.bold,
                          fontSize: 16,
                        ),
                      ),
                    );
                  })
            ],
          ),
          Padding(
            padding: EdgeInsets.symmetric(horizontal: 10),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                buildButton(
                    context: context,
                    onPressCallBack: (){},
                    childWidgetBuilder: (context){
                      return Padding(
                        padding: EdgeInsets.all(6),
                        child: Text(
                          '目录'
                        ),
                      );
                    }
                ),
                buildButton(
                    context: context,
                    onPressCallBack: (){},
                    childWidgetBuilder: (context){
                      return Padding(
                        padding: EdgeInsets.all(6),
                        child: Text(
                            '选项'
                        ),
                      );
                    }
                ),
                buildButton(
                    context: context,
                    onPressCallBack: (){},
                    childWidgetBuilder: (context){
                      return Padding(
                        padding: EdgeInsets.all(6),
                        child: Text(
                            '亮度'
                        ),
                      );
                    }
                ),
                buildButton(
                    context: context,
                    childWidgetBuilder: (context){
                      return Padding(
                        padding: EdgeInsets.all(6),
                        child: Text(
                            '阅读模式'
                        ),
                      );
                    },
                  onPressCallBack: (){
                      var viewModel = Get.find<NovelContentChapterViewModel>(
                        tag: widget.bookid.toString()
                      );
                      //仿真翻页
                      if(viewModel.currentTurnMode == ReaderTurnPageMode.simulationMode){
                        viewModel.changeTurnMode(ReaderTurnPageMode.coverMode);
                      }else{
                        viewModel.changeTurnMode(ReaderTurnPageMode.simulationMode);
                      }
                  }
                )
              ],
            ),
          )
        ],
      ),
    );
  }
}


